/******************************************************************************
 * * demo_main.c - implementation of 128*64 demo
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/

#include "demo_global.h"

#if USE_LV_DEMO_128x64

extern INT8 *Framework_Signal_Bar_To_Symbol(MMI_MODEM_SIGNAL_BAR Bar);
extern INT8 *Framework_Get_Battery_Symbol(UINT8 BatSta);
extern UINT8 Framework_Get_Battery_Level(UINT8 Bat);

/**********************
 *  STATIC FUNCTIONS
 **********************/
static VOID Demo_Status_Update_Cb(VOID *param)
{
    UI_Demo_Desc_t *pDemoDesc = (UI_Demo_Desc_t *)param;

    //Update Signal
    lv_label_set_text(pDemoDesc->SignalLabel.Label,
            Framework_Signal_Bar_To_Symbol(MMI_Modem_Get_Signal_Bar_Req(MMI_MODEM_SIM_1)));

    //Update Battery
    HAL_CHG_STATUS ChaSta = Hal_Charger_Get_Status();
    if (ChaSta == HAL_CHG_DISCONNECTED)
    {
        lv_label_set_text(pDemoDesc->BatteryLabel.Label,
                Framework_Get_Battery_Symbol(Framework_Get_Battery_Level(Hal_Battery_Get_Status())));
    }
    else if (ChaSta == HAL_CHG_CONNECTED)
    {
        lv_label_set_text(pDemoDesc->BatteryLabel.Label, LV_SYMBOL_CHARGE);
    }

    //Update RAT
    INT8 Operator[40];
    MMI_MODEM_PLMN_RAT Rat;
    MMI_Modem_Get_Operator_Req(MMI_MODEM_SIM_1, Operator, &Rat);

    if(MMI_MODEM_PLMN_RAT_LTE == Rat) {
        lv_label_set_text(pDemoDesc->RatLabel.Label, "4G");
    } else if(MMI_MODEM_PLMN_RAT_GSM == Rat) {
        lv_label_set_text(pDemoDesc->RatLabel.Label, "2G");
    } else {
        lv_label_set_text(pDemoDesc->RatLabel.Label, "");
    }

    //Update Date
    INT8                            Str[20];
    hal_rtc_t                       Rtc;
    Hal_Rtc_Gettime(&Rtc);
    snprintf(Str, 20, "%02d-%02d",
            Rtc.tm_mon,
            Rtc.tm_mday);
    lv_label_set_text(pDemoDesc->DateLabel.Label, Str);

    //Update Time
    snprintf(Str, 20, "%02d:%02d",
            Rtc.tm_hour,
            Rtc.tm_min);
    lv_label_set_text(pDemoDesc->TimeLabel.Label, Str);
}

static VOID Demo_OnCreate(VOID *pDesc)
{
    UI_Demo_Desc_t *pDemoDesc = (UI_Demo_Desc_t *)pDesc;

    //Set RAT
    INT8 Operator[40];
    MMI_MODEM_PLMN_RAT Rat;
    MMI_Modem_Get_Operator_Req(MMI_MODEM_SIM_1, Operator, &Rat);

    if(MMI_MODEM_PLMN_RAT_LTE == Rat) {
        lv_label_set_text(pDemoDesc->RatLabel.Label, "4G");
    } else if(MMI_MODEM_PLMN_RAT_GSM == Rat) {
        lv_label_set_text(pDemoDesc->RatLabel.Label, "2G");
    } else {
        lv_label_set_text(pDemoDesc->RatLabel.Label, "");
    }

    //Set Date
    INT8                            Str[20];
    hal_rtc_t                       Rtc;
    Hal_Rtc_Gettime(&Rtc);
    snprintf(Str, 20, "%02d-%02d",
            Rtc.tm_mon,
            Rtc.tm_mday);
    lv_label_set_text(pDemoDesc->DateLabel.Label, Str);

    //Set Time
    snprintf(Str, 20, "%02d:%02d",
            Rtc.tm_hour,
            Rtc.tm_min);
    lv_label_set_text(pDemoDesc->TimeLabel.Label, Str);

    //Start Timer
    Hal_Timer_Start(1000, Demo_Status_Update_Cb, pDesc, TRUE);
}


/**********************
 *   GLOBAL FUNCTIONS
***********************/

/**
 * Create 128*64 demo
 * param (in) VOID
 * return  VOID
 */
VOID Demo_128x64_Create(VOID)
{
    Nav_Func_List_t  FuncList;
    UI_Demo_Desc_t *pDesc;

    FuncList.OnShow         = (Void_P_Func_t)UI_Demo_128x64_Create;
    FuncList.OnHide         = NULL;
    FuncList.OnCreate       = Demo_OnCreate;
    FuncList.OnDestroy      = NULL;
    FuncList.OnSaveState    = NULL;
    FuncList.OnRestoreState = NULL;

    pDesc = (UI_Demo_Desc_t *)Hal_Mem_Alloc(sizeof(UI_Demo_Desc_t));
    memset(pDesc, 0, sizeof(UI_Demo_Desc_t));

    pDesc->SignalLabel.TxtId = LV_LANG_TXT_ID_NONE;
    pDesc->SignalLabel.TxtAlign = LV_LABEL_ALIGN_LEFT;
    pDesc->SignalLabel.SymbolId = Framework_Signal_Bar_To_Symbol(MMI_Modem_Get_Signal_Bar_Req(MMI_MODEM_SIM_1));

    pDesc->RatLabel.TxtId = LV_LANG_TXT_ID_NONE;
    pDesc->RatLabel.TxtAlign = LV_LABEL_ALIGN_LEFT;
    pDesc->RatLabel.SymbolId = NULL;

    pDesc->BatteryLabel.TxtId = LV_LANG_TXT_ID_NONE;
    pDesc->BatteryLabel.TxtAlign = LV_LABEL_ALIGN_LEFT;
    HAL_CHG_STATUS ChaSta = Hal_Charger_Get_Status();
    if (ChaSta == HAL_CHG_DISCONNECTED)
    {
        pDesc->BatteryLabel.SymbolId = Framework_Get_Battery_Symbol(Framework_Get_Battery_Level(Hal_Battery_Get_Status()));
    }
    else if (ChaSta == HAL_CHG_CONNECTED)
    {
        pDesc->BatteryLabel.SymbolId = LV_SYMBOL_CHARGE;
    }

    pDesc->DateLabel.TxtId = LV_LANG_TXT_ID_NONE;
    pDesc->DateLabel.TxtAlign = LV_LABEL_ALIGN_LEFT;
    pDesc->DateLabel.SymbolId = NULL;

    pDesc->TimeLabel.TxtId = LV_LANG_TXT_ID_NONE;
    pDesc->TimeLabel.TxtAlign = LV_LABEL_ALIGN_LEFT;
    pDesc->TimeLabel.SymbolId = NULL;

    Nav_Forward(ACT_ID_ANY, ACT_ID_DEMO_128x64, &FuncList, pDesc);
}

#endif //USE_LV_DEMO_128x64
